/* eslint-disable */
import $http from '../utils/http'
import { Notification } from 'AtElement';


// 函数名
// 获得任务列表
const gettasklist = 'taskManagement/gettasklist';
// 获得任务详情
const gettaskdetails = 'taskManagement/gettaskdetails'
// 获得节点信息
const getallnode = 'taskManagement/getallnode'
// 获得节点信息
const getallagency = 'taskManagement/getallagency'
// 任务配置项
const configBaseItems = 'taskManagement/configBaseItems'
// 新增任务
const addtask = 'taskManagement/addtask'
// 下发任务
const assignment = 'taskManagement/assignment'
// 流量监测的数据包
const getflowmontor = 'taskManagement/getflowmontor'



// 变量名
// 获得任务列表
const gettasklistdata = 'taskManagement/gettasklistdata';
const gettaskconfigBase = 'taskManagement/gettaskconfigBase';
const getallnodemsg = 'taskManagement/getallnodemsg';
const getallagencymsg = 'taskManagement/getallagencymsg';
// 对列表的分页、筛选、排序、搜索
const changetaskCurrentPage = 'taskManagement/changetaskCurrentPage';
const changetaskPageSize = 'taskManagement/changetaskPageSize';
const changetaskSorter = 'taskManagement/changetaskSorter'
const changetaskSearch = 'taskManagement/changetaskSearch'
const changetaskFilters = 'taskManagement/changetaskFilters'
// 任务详情页
const taskdetails = 'taskManagement/taskdetails'
const tabflow = 'taskManagement/tabflow'

const taskManagement = {
	state: {
		// 任务配置项
		tableTaskcogfig: [],
		// 获得机构项
		tableTgencymsg: [],
		// 获得节点项
		tableNode: [],
    // 任务列表
		tableTasklist: {
            total: 0,
            dataSource: []
        },
    tableTaskdetails: {
			task_details: [{
				task_name: '',
				task_id :'',
				task_state: '',
				task_type : '',
				time_range: '',
				task_source: '',
				feedback_time: '',
				person_name: '', 
				aim_names: '',
				issue_time: ''
			}],
			task_config: [{
				task_config: []
			}]
		},
		// 任务列表传的参数
		parameterTasklist: {
			pagination: {
				current: 1, // 当前页码
				pageSize: 20 // 当前页显示的数据条数
			},
			search: {
				value: '' // 需要模糊搜索的值（搜索框）
			},
			sorter: {
				field: 'br_time', // 排序的字段
				order: 'ascing' // 升序还是降序
			},
			filters: {
				// 需要被过滤的字段
				btask_type: [],
				btask_state: [],
				btask_source: []
			}
		},
		// 流量监测
		tableflow: [],
    },
    
	actions: {
		// 任务配置
		[configBaseItems]({ commit }, agency_id){
			$http.post('/api/task/configBaseItems', { 'agency_id': agency_id }).then(({ status, msg, data } = {}) =>{
				if (status === 'success') {
				  commit("taskManagement/gettaskconfigBase", data)
				} else if(status === 'fail') {
				  Notification.error({
					title: '数据获取异常',
					message: msg
				  })
				}
			  })
				},
		// 任务配置
		[getallnode]({ commit }){
			$http.post('/api/task/getallnode').then(({ status, msg, data } = {}) =>{
				if (status === 'success') {
				  commit("taskManagement/getallnodemsg", data)
				} else if(status === 'fail') {
				  Notification.error({
					title: '数据获取异常',
					message: msg
				  })
				}
			  })
				},
		// 任务配置
		[getallagency]({ commit }){
			$http.post('/api/task/getallagency').then(({ status, msg, data } = {}) =>{
				if (status === 'success') {
				  commit("taskManagement/getallagencymsg", data)
				} else if(status === 'fail') {
				  Notification.error({
					title: '数据获取异常',
					message: msg
				  })
				}
			  })
        },
		// 任务列表
		[gettasklist]({ commit , state }){
			$http.option('/api/task/tasklist', state.parameterTasklist).then(({ status, msg, data } = {}) =>{
				if (status === 'success') {
				  commit("taskManagement/gettasklistdata", data)
				} else if(status === 'fail') {
				  Notification.error({
					title: '数据获取异常',
					message: msg
				  })
				}
			  })
        },
        
    // 任务详情
		[gettaskdetails]({ commit }, task_id){
			$http.post('/api/task/taskdetails', {'task_id':task_id} ).then(({ status, msg, data } = {}) =>{
				if (status === 'success') {
				  commit("taskManagement/taskdetails", data)
				} else if(status === 'fail') {
				  Notification.error({
						title: '数据获取异常',
						message: msg
				  })
				}
			  })
		},
		// 新增任务
		[addtask] ({ dispatch }, taskMsg) {
			$http.post('/api/task/addtask', 
			           {'task_msg': taskMsg['task_msg'],
								 'task_target':  taskMsg['task_target'],
								 'task_element': taskMsg['task_element'] } ).then(({ status, msg, task_id } = {}) =>{
			if (status === 'success') {
					 dispatch('taskManagement/gettasklist')
					 dispatch('taskManagement/assignment', task_id )
				} else if(status === 'fail') {
				  Notification.error({
					title: '数据获取异常',
					message: msg
				  })
				}
			  })
		},

		// 任务下发
		[assignment] ( {} ,task_id ) {
      $http.post('/api/task/assignment', {'task_id':task_id} ).then(({ status, msg } = {}) =>{
				if (status === 'success') {
				  Notification.success('任务下发成功')
				} else if(status === 'fail') {
				  Notification.error({
						title: '数据获取异常',
						message: msg
				  })
				}
			  })
		},
		// 流量监测的数据
		[getflowmontor] ({commit},task_id) {
      $http.post('/api/task/flowMontrior', {'task_id': task_id}).then(({ data, status, msg } = {}) => {
        if (status === 'success') {
					commit('taskManagement/tabflow', data)
        } else if (status === 'fail') {
				  Notification.error({
            title: '数据获取异常',
            message: msg
				  })
        }
      })
    }
	},
	mutations: {
		[gettasklistdata](state, data) {
			state.tableTasklist = data
        },
    [changetaskCurrentPage](state, data) {
			state.parameterTasklist.pagination.current = data
		},
		[changetaskPageSize] (state, data) {
      state.parameterTasklist.pagination.pageSize = data
		},
    [changetaskSorter](state, data) {
			state.parameterTasklist.sorter = data
    },
    [changetaskSearch](state, data) {
			state.parameterTasklist.search.value = data
        },
    [changetaskFilters](state, data) {
            for(var key in data ){
                if (key=='btask_state'){
                   state.parameterTasklist.filters.btask_state = data.btask_state
								}
								else if(key=='btask_type'){
									state.parameterTasklist.filters.btask_type = data.btask_type
								}
                else{
									state.parameterTasklist.filters.btask_source = data.btask_source 
                }
            }
        },
    [taskdetails] (state, data) {
      state.tableTaskdetails = data
		},
		
		[gettaskconfigBase](state, data) {
      state.tableTaskcogfig = data
		},
    [tabflow](state, data){
      state.tableflow = data
		},
		[getallnodemsg](state, data) {
			state.tableNode = data
		},
		[getallagencymsg](state, data) {
			state.tableTgencymsg = data
		}
	}
};

export default taskManagement;
